<?php

/**
 * 文件由尚数增量代码引擎生成
 * @author: Shann 851188611@qq.com
 */

namespace app\admin\controller;

class CouponBase
{
    public function index()
    {
        $coupons = \app\model\Coupon::field(['id', 'name', 'started_at', 'ended_at']);

        $coupons->order('id', 'desc');

        $data['coupons'] = $coupons->select();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function detail()
    {
        $coupon = \app\model\Coupon::field(['id', 'name', 'started_at', 'ended_at']);

        $coupon->where('id', input('id'));

        $data['coupon'] = $coupon->find();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function create()
    {
        $param = request()->param();

        $model = new \app\model\Coupon();

        $model->save($param);

        $data['coupon'] = \app\model\Coupon::find($model->id);

        return json(['code' => 20000, 'msg' => 'success', 'data' => $data]);
    }

    public function update()
    {
        $param = request()->param();

        $model = \app\model\Coupon::find(input('id'));

        $model->save($param);

        return json(['code' => 20000, 'msg' => 'success']);
    }

    public function delete()
    {
        \app\model\Coupon::destroy(function ($query) {
            $query->where('id', input('id'));
        });

        return json(['code' => 20000, 'msg' => 'success']);
    }
}
